
package com.kim.ping.mq

import com.kim.ping.Service.PingService
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently
import org.apache.rocketmq.common.message.MessageExt
import org.mockito.Mockito
import org.slf4j.LoggerFactory
import spock.lang.Specification

import java.nio.charset.StandardCharsets

class CosumerSpec extends Specification {

    def logger = LoggerFactory.getLogger(CosumerSpec.class)
    def pingService = Mock(PingService)
    def consumer = Mock(DefaultMQPushConsumer)
    def cosumer = new Cosumer(service: pingService, consumer: consumer)

    def setup() {
        cosumer.consumer = consumer
    }

    def "start should configure and start the consumer correctly"() {
        given:
        def messageExt = Mockito.mock(MessageExt.class)
        Mockito.when(messageExt.getBody()).thenReturn("Test Message".getBytes(StandardCharsets.UTF_8))

        when:
        cosumer.start()

        then:
        1 * consumer.setNamesrvAddr("127.0.0.1:9876")
        1 * consumer.subscribe("Rate1", "*")
        1 * consumer.registerMessageListener(_ as MessageListenerConcurrently)
        1 * consumer.start()
    }

}
